/*
    Copyright (c) Guangzhou Data Control Network Technology Co., Ltd. All rights reserved.
    Licensed under the MIT License.
    contact:dcdev_founder@foxmail.com
*/
// 插入样式
css = `.layui-form-label{text-align:left;}.layui-form-label::before{content:'';width:0;height:26px;border-left:3px solid #1296db;position:absolute;left:0;top:6px;}`
style = document.createElement("style");
style.appendChild(document.createTextNode(css));
document.getElementsByTagName("head")[0].appendChild(style);

function open(data, cb) {
    // 调整窗体宽高
    {
        if (browser_info.is_mobile) {
            w = data.appear.mobile.size.w;
            h = data.appear.mobile.size.h;
            // 如果是百分比
            if (data.appear.mobile.size.w.charAt((data.appear.mobile.size.w).length - 1) == "%") {
                w = parseInt(w.substr(0, w.length - 1))
                w = (window.screen.width * w) / 100 + "px"
            }
            if (data.appear.mobile.size.h.charAt((data.appear.mobile.size.h).length - 1) == "%") {
                h = parseInt(h.substr(0, h.length - 1))
                h = (window.screen.height * h) / 100 + "px"
            }
            offset_x = data.appear.mobile.offset.x;
            offset_y = data.appear.mobile.offset.y
        } else {
            w = data.appear.pc.size.w;
            h = data.appear.pc.size.h;
            // 如果是百分比
            if (data.appear.pc.size.w.charAt((data.appear.pc.size.w).length - 1) == "%") {
                w = parseInt(w.substr(0, w.length - 1))
                w = window.screen.width * w / 100 + "px"
            }
            if (data.appear.pc.size.h.charAt((data.appear.pc.size.h).length - 1) == "%") {
                h = parseInt(h.substr(0, h.length - 1))
                h = window.screen.height * h / 100 + "px"
            }
            offset_x = data.appear.pc.offset.x;
            offset_y = data.appear.pc.offset.y
        }
    }

    content = `
    <iframe id="contentIframe" style="width:100%;height:100%"
            src='${data.url}'
            frameborder="0"></iframe>
    `
    var form_config = {
        area: [w, h],
        id: 'dcdev-popup-form',
        shade: data.mask,
        btn2: function () { },
        content: content,
        success: function (layero) {
            // uiClass.hideLoading();
            parent.$('.layui-layer-btn-c').html(`<div id="closebtn" style="text-align:center;"><i class="layui-icon layui-icon-close-fill" style="font-size:40px;color:#fff"></i></div>`)
            parent.$('.layui-layer-btn-c').css('margin-top', '10px')
            // 有偏移值
            parent.$('#closebtn').click(function () {
                parent.layer.closeAll();
                cb();
            })
            // 如果有设置偏移值
            if (offset_x && offset_y) {
                parent.$('#dcdev-popup-form').parent().css('top', offset_y)
                parent.$('#dcdev-popup-form').parent().css('left', offset_x)
            }
            // 判断其它
            else {
                // 手机端对于显示一些人性化处理
                if (browser_info.is_andriod || browser_info.is_ios) {
                    // 宽度是百分比
                    if (data.appear.mobile.size.w.charAt(data.appear.mobile.size.w.length - 1) == "%") {
                        // 如果宽设置了100%，则整个弹出页面置顶
                        if (data.appear.mobile.size.w == "100%") {
                            offset_y = "0px";
                        }
                        else {
                            // 如果宽度是百分比，且高度百分比超过60%，则往上偏移（以尽量能看到关闭按钮），否则居中
                            if (parseInt(data.appear.mobile.size.h.replace("%", "")) > 60) {
                                offset_y = "10px";
                            }
                        }
                        parent.$('#dcdev-popup-form').parent().css('top', offset_y);
                    }
                }
                else {
                    // 宽度是百分比
                    if (data.appear.pc.size.w.charAt(data.appear.pc.size.w.length - 1) == "%") {
                        // 如果宽设置了100%，则整个弹出页面置顶
                        if (data.appear.pc.size.w == "100%") {
                            offset_y = "0px";
                        }
                    }
                    // 宽度是百分比
                    if (data.appear.pc.size.h.charAt(data.appear.pc.size.h.length - 1) == "%") {
                        // 如果高度百分比超过60%，则往上偏移，否则居中
                        if (parseInt(data.appear.pc.size.h.replace("%", "")) > 60) {
                            offset_y = "10px";
                        }
                    }
                    parent.$('#dcdev-popup-form').parent().css('top', offset_y);
                }
            }
            parent.$('#dcdev-popup-form').css('height', h)

            if (browser_info.is_andriod) {
                //获取原窗口的高度
                var originalHeight = document.documentElement.clientHeight || document.body.clientHeight;
                window.onresize = function () {
                    //键盘弹起与隐藏都会引起窗口的高度发生变化
                    var resizeHeight = document.documentElement.clientHeight || document.body
                        .clientHeight;
                    if (resizeHeight - 0 >= originalHeight - 0) {
                        document.body.scrollTop = $(document).height() - $(document).scrollTop();
                    }
                }
            }
            if (browser_info.is_ios) {
                parent.document.body.addEventListener('focusout', function () { //软键盘关闭事件
                    parent.document.body.scrollTop = parent.$(document).height() - parent.$(
                        document).scrollTop();
                });
            }
        },
        end: function () {

        }
    }

    // 根据配置弹出页面
    popupForm(form_config);

    //监听手机屏幕翻转
    window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize", handleRotage, false);
    function handleRotage() {
        popupForm(form_config);
        parent.$('#dcdev-popup-form').parent().css('top', offset_y);
    }
    function popupForm(set, config = null) {
        if (!config) {
          config = {};
          if (browser_info.is_mobile) {
            config.width = "92%", config.height = "92%";
          }
          else {
            config.width = "80%", config.height = "70%";
          }
        }
        // 有配置，但要确定是否有该值
        else {
          if (!config.width) {
            if (browser_info.is_mobile) {
              config.width = "92%";
            }
            else {
              config.width = "80%";
            }
          }

          if (!config.height) {
            if (browser_info.is_mobile) {
              config.height = "92%";
            }
            else {
              config.height = "70%";
            }
          }
        }
        if (set.margin) {
          set.area = [];
          for (var i = 0; i < set.margin.length; i++) {
            if (i == 0) {
              set.area.push(($(parent.window).width() - parseInt(set.margin[i]) * 2) + 'px')
            } else {
              set.area.push(($(parent.window).height() - parseInt(set.margin[i]) * 2) + 'px')
            }
          }
          delete set.margin;
        }
        var obj = {
          type: 1
          , title: false //不显示标题栏
          , closeBtn: false
          , area: [config.width, config.height] //宽高
          , offset: 'auto'   //定位
          , scrollbar: false
          , shade: 0.001  //蒙层
          , id: 'LAY_layuipro' //设定一个id，防止重复弹出
          , btn: [config.config_button ? config.config_button : '确定', config.cancel_button ? config.cancel_button : '取消']
          , btnAlign: 'c'  //按钮居中
          , moveType: 1 //拖拽模式，0或者1
        }
        Object.assign(obj, set)
        layui.use('layer', function () {
          var layer = layui.layer;
          parent.layer.open(obj)
        })
      }
      
}
